package com.ls.service.impl;

import com.ls.config.AliYunConfig;
import com.ls.dto.UserDto;
import com.ls.mapper.UserMapper;
import com.ls.pojo.User;
import com.ls.service.UserService;
import com.ls.utils.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 添加用户
     * @param userDto
     * @return
     */
    @Override
    public Boolean insert(UserDto userDto) {

        try {
            userMapper.insert(userDto);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 修改用户
     * @param userDto
     * @return
     */
    @Override
    public Boolean update(UserDto userDto) {

        try {
            return userMapper.updPwd(userDto.getPassword())>0;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 修改密码
     * @param userDto
     * @return
     */
    @Override
    public Boolean updPwd(UserDto userDto) {
        return null;
    }

    /**
     * 删除用
     * @param id
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        try {
            return userMapper.delete(id)>0;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 查询所有用户
     * @return
     */
    @Override
    public List<User> queryAll() {
        return userMapper.queryAll(null);
    }

    /**
     * 通过id查询用户
     * @param id
     * @return
     */
    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    /**
     * 通过用户名查询用户
     * @param userDto
     * @return
     */
    @Override
    public User getUser(UserDto userDto) {
        return userMapper.getUser(userDto);
    }

    /**
     * 判断验证码是否正确
     * @param code
     * @param captcha
     * @return
     */
    @Override
    public Boolean codeIsTrue(String code, String captcha) {

        return code.equalsIgnoreCase(captcha);
    }

    /**
     * 验证登录
     *
     * @param userDto
     * @return
     */
    @Override
    public Status login(UserDto userDto, HttpSession session) {

        try {
            //用户名正确
            if (userMapper.checkLogin(userDto)>0){
                User user = userMapper.getUserByUserName(userDto.getUsername());
                session.setAttribute("user",user);
                return new Status(true,"登录成功~");
            }
        } catch (Exception e) {
            return new Status(false,"数据错误~");
        }

        return new Status(false,"验证吗或密码错误~");
    }

    /**
     * 添加用户
     * @param userDto
     * @return
     */
    @Override
    public Status insertResult(UserDto userDto) {

        if (insert(userDto)){
            return new Status().setStatus(true).setMsg("添加成功~");
        }else {
            return new Status().setStatus(false).setMsg("添加失败~");
        }

    }

    @Override
    public Status uploadImage(MultipartFile file,HttpSession session) {

        String contentType = file.getContentType();

        String imageName = null;

        if ("image/jpg".equals(contentType)||"image/jpeg".equals(contentType)||"image/png".equals(contentType)){
            User user = (User)session.getAttribute("user");

            String prefix = user.getUsername() + "/avatar";

            try {
                imageName = AliYunConfig.uploadImage(file,prefix);
                userMapper.updAvatar(imageName);
                return new Status().setStatus(true).setMsg("更换成功~");
            } catch (IOException e) {
                return new Status().setStatus(false).setMsg("操作失败~");
            }
        }else {
            return new Status().setStatus(false).setMsg("图片格式不正确~");
        }
    }
}
